﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SignalShopBase
{
    public class Complex
    {
        private double x;
        public double X { get { return x; } set { this.x = value; } }

        private double y;
        public double Y { get { return y; } set { this.y = value; } }

        public double Abs { get { return Math.Sqrt(x * x + y * y); } }
        public double Arg { get { return Math.Atan2(y, x); } }

        public Complex()
        {
            Set(0, 0);
        }

        public Complex(double x, double y)
        {
            Set(x, y);
        }

        public Complex(Complex c)
        {
            Set(c.X, c.Y);
        }

        public Complex Set(double x, double y)
        {
            this.x = x;
            this.y = y;

            return this;
        }

        public Complex Set(Complex c)
        {
            return Set(c.X, c.Y);
        }

        public Complex Nrm()
        {
            double abs = Math.Sqrt(x * x + y * y);

            this.x /= abs;
            this.y /= abs;

            return this;
        }

        public Complex Inv()
        {
            double abs2 = Abs*Abs;
            double x1 = x / abs2;
            double y1 = -y / abs2;
            x = x1; y = y1;

            return this;
        }

        public Complex Mul(double c)
        {
            this.x *= c;
            this.y *= c;

            return this;
        }

        public Complex Rot(double alpha)
        {
            double x1 = Math.Cos(alpha) * x - Math.Sin(alpha) * y;
            double y1 = Math.Sin(alpha) * x + Math.Cos(alpha) * y;

            this.x = x1;
            this.y = y1;

            return this;
        }

        public Complex Unit(double alpha)
        {
            this.x = Math.Cos(alpha);
            this.y = Math.Sin(alpha);

            return this;
        }

        // conjugate
        public Complex Cjg()
        {
            this.y *= -1;
            return this;
        }

        public Complex Add(double x, double y)
        {
            this.x += x;
            this.y += y;

            return this;
        }

        public Complex Add(Complex c)
        {
            return Add(c.X, c.Y);
        }

        public Complex Sub(double x, double y)
        {
            this.x -= x;
            this.y -= y;

            return this;
        }

        public Complex Sub(Complex c)
        {
            return Sub(c.X, c.Y);
        }

        public Complex Mul(double x, double y)
        {
            double x1 = this.x * x - this.y * y;
            double y1 = this.x * y + this.y * x;

            this.x = x1;
            this.y = y1;

            return this;
        }

        public Complex Mul(Complex c)
        {
            return Mul(c.X, c.Y);
        }

        public override string ToString()
        {
            return String.Format("({0}, {1})", x, y);
        }
    }
}
